package services

import (
	"micro-bbs/initialize"
	"micro-bbs/libs"
	"micro-bbs/mqueue"
	"micro-bbs/repositories"
	"time"
)

/*
	提供关于RabbitMQ服务

	作者名称：leixiaotian 创建时间：20211124
*/
type RabbitmqInterfaceService interface {
	PushLoginLog(id int64) (interface{},error) //推送登录日志MQ
	PushConfirmPost(id int64) (interface{},error) //推送发帖确认MQ
	PullFinanceArt(number int)                       //处理mq金融文章
}

//初始化对象函数
func NewRabbitmqService() RabbitmqInterfaceService {
	return &rabbitmqService{
		bbsUserService:          repositories.NewBbsUser(),
		rabbitMqService: 		 mqueue.NewRabbitService(),
	}
}

type rabbitmqService struct {
	bbsUserService 			    repositories.BbsUserInterface           //社区会员服务
	rabbitMqService 			mqueue.RabbitInterfaceService           //rabbitMQ服务
}

//推送登录日志MQ
func (this *rabbitmqService) PushLoginLog(id int64) (interface{}, error) {
	userInfo, err := this.bbsUserService.SelectInfo(id)
	initialize.IrisLog.Infof("[mq服务-userInfo数据]-[%s]", libs.StructToJson(userInfo))
	if err != nil {
		initialize.IrisLog.Errorf("[mq服务-获取帖子信息失败]-[%s]", err.Error())
		return 3006, err
	}
	this.rabbitMqService.PushLoginLog(userInfo)
	return true, nil
}

//推送发帖确认MQ
func (this *rabbitmqService) PushConfirmPost(id int64) (interface{}, error) {
	initialize.IrisLog.Infof("[mq服务-recordId]-[%d]", id)
	this.rabbitMqService.PushUserPost(id)
	return true, nil
}

//处理mq金融文章
func (this *rabbitmqService) PullFinanceArt(number int) {
	//创建channel
	shopCh, err := initialize.MqClient.GetChannel()
	if err != nil {
		initialize.IrisLog.Infof("[消费金融文章-创建channel失败]-[%s]", err.Error())
		return
	}
	defer shopCh.Close()

	//队列
	q, err := shopCh.QueueDeclare(
		initialize.Config.GetString("RabbitMQ.FinanceArticleQueue"), //name
		true,  //durable
		false, //delete when usused
		false, //exclusive
		false, //nowait
		nil,   //argments
	)
	if err != nil {
		initialize.IrisLog.Errorf("[消费金融文章-创建queue失败]-[%s]", err.Error())
		return
	}

	//消费队列
	msgs, err := shopCh.Consume(
		q.Name, // queue
		"",     // consumer
		false,  // auto-ack
		false,  // exclusive
		false,  // no-local
		false,  // no-wait
		nil,    // args
	)
	if err != nil {
		initialize.IrisLog.Errorf("[消费金融文章-创建消费者失败]-[%s]", err.Error())
		return
	}

	forever := make(chan bool)
	go func() {
		for d := range msgs {
			time.Sleep(20 * time.Millisecond)
			//开始事务处理各种数据
			//绑定参数
			//var article datamodels.FinanceArticle
			//if err := json.Unmarshal(d.Body, &article); err != nil {
			//	initialize.IrisLog.Errorf("[消费金融文章-json数据失败]-[%s]", err.Error())
			//	continue
			//}
			//initialize.IrisLog.Infof("[消费金融文章-mq信息文章ID：]-[%s]", libs.StructToJson(article.ID))
			////MySQL文档记录
			//var lxtArticle datamodels.LxtArticle
			//lxtArticle.ID = 0
			//lxtArticle.Uid = 1
			//lxtArticle.Name = strings.Trim(article.Source," ")
			//lxtArticle.Title = article.Title
			//lxtArticle.CategoryId = article.CategoryId
			//lxtArticle.ThirdId = article.ID
			//lxtArticle.ThirdUrl = article.Url
			//lxtArticle.ThirdDate = article.CreateDate
			//lxtArticle.Type = 2
			////lxtArticle.Description = ""
			//lxtArticle.ModelId = 2
			//lxtArticle.Position = 1
			//lxtArticle.Deadline = time.Now().Unix()
			//lxtArticle.IsHot = 1
			//lxtArticle.IsTop = 1
			//lxtArticle.CreateTime = article.CreateTime
			//lxtArticle.UpdateTime = article.CreateTime
			//lxtArticle.Status = 1
			///*
			//	MySQL文档内容
			//*/
			//var lxtArticleContent datamodels.LxtArticleContent
			//lxtArticleContent.DocId = 0
			//lxtArticleContent.Content = article.Content
			//
			////插入文章和内容记录
			//logId, err := this.LxtArticleService.InsertFinance(lxtArticle, lxtArticleContent)
			//if err != nil {
			//	//失败了 记录失败日志
			//	jsonBytes, _ := json.Marshal(lxtArticle)
			//	var recordLog datamodels.ExceptionRecordLog
			//	recordLog.ID = 0
			//	recordLog.Oid = article.ID
			//	recordLog.Name = article.Title
			//	recordLog.Type = 1 //1:爬虫文章插入失败;
			//	recordLog.CreateTime =time.Now().Unix()
			//	recordLog.Status = 1
			//	recordLog.Content = string(jsonBytes)
			//	recordLog.Remark = "爬虫金融文章插入失败:"+err.Error()
			//	if err := this.exceptionRecordLogService.Insert(recordLog); err != nil {
			//		initialize.IrisLog.Errorf("[消费金融文章-插入错误日志失败]-[%s]", string(jsonBytes))
			//		continue
			//	}
			//}
			//如果Auto ack设为false。consumer在处理完消息后，调用msg.Ack(false)后消息才从queue中去除。
			//即便当前消费者处理该消息发生意外，只要没有执行msg.Ack(false)那该消息就仍然在queue中，不会丢失。
			//if logId > 0 {
			//	initialize.IrisLog.Infof("[消费金融文章-消费成功]-[%d]", logId)
			//	d.Ack(false)
			//}

			d.Ack(false)
		}
	}()
	initialize.IrisLog.Infof(" [消费金融文章-MQ]-线程[%d]", number)
	<-forever
}